<?php
	include("library/vars.php");
	
	if(isset($_POST['submit'])) { // If the form is submited
	
		$episode_nr = $_POST['episode_nr'];
		$title = addslashes($_POST['title']);
		$description = addslashes($_POST['description']);
	
		include("library/config.php");
		include("library/opendb.php");
	
		if ($episode_nr==$episode) { // Check to see if we haven't changed the episode number
			$query = "UPDATE episodes SET show_ID='$show', season_ID='$season', episode='$episode_nr', title='$title', description='$description' WHERE episode='$episode' AND season_ID='$season' AND show_ID='$show'";
			mysql_query($query) or die('Database Error');

			include 'library/closedb.php';
			header( 'Location: index.php?s=' . $show . '&t=' . $season . '&m=episode_updated' );
		}
		else { // If we changed the episode number, check to see if there's already an episode with that number
			$query="SELECT * FROM episodes WHERE episode='$episode_nr' AND season_ID='$season' AND show_ID='$show'";
			$result = mysql_query($query);
			$num=mysql_numrows($result);
	
			if ($num==0) { // If not, edit the episode
				$query = "UPDATE episodes SET show_ID='$show', season_ID='$season', episode='$episode_nr', title='$title', description='$description' WHERE episode='$episode' AND season_ID='$season' AND show_ID='$show'";
				mysql_query($query) or die('Database Error');

				include 'library/closedb.php';
				header( 'Location: index.php?s=' . $show . '&t=' . $season . '&m=episode_updated' );
			} else {
				include 'library/closedb.php';
				header( 'Location: edit_episode.php?s=' . $show . '&t=' . $season . '&e=' . $episode . '&m=episode_already_exists2' );
			}
		}	
	}
	
	else { // Show the form, filled with the original data
		
		include("library/config.php");
		include("library/opendb.php");
		
		$query="SELECT * FROM episodes WHERE episode='$episode' AND season_ID='$season' AND show_ID='$show'";
		$result = mysql_query($query);

		$query2="SELECT id, show_name, show_nicename path FROM shows WHERE show_nicename='$show'";
		$result2 = mysql_query($query2);

		$query3="SELECT season, show_ID FROM seasons WHERE season='$season' AND show_ID='$show'";
		$result3 = mysql_query($query3);
		
		$i=0;
		$showname=mysql_result($result2,$i,"show_name");
		$season_name=mysql_result($result3,$i,"season");
		$episode_nr=mysql_result($result,$i,"episode");
		$title=mysql_result($result,$i,"title");
		$description=mysql_result($result,$i,"description");
		
		$title=htmlspecialchars($title);
		$description=htmlspecialchars($description);
				
		include("library/closedb.php");	
		include("library/header.php");
	}
	
	if(isset($_POST['get'])) { // If selected to get episode details...
	
		$episode_nr = $_POST['episode_nr'];
		
		if ($episode_nr > 0) {
			$getshow = $show;
			include("library/parse_fix.php");
			include("library/get_episode.php");
			
			$description = $summary;
		}
						
	}
	
?>
	
<div id="main">
	<div id="seasons">
		<h2><?php echo _("Editing Episode for"); ?>: <strong><?=$showname?> <small>(Season <?=$season_name?>)</small></strong></h2>
	</div>
		
	<div id="messages">
		<?php include ("library/messages.php");	?>
	</div>
	
	<form method="post" id="edit" autocomplete="off" action="">

			

		<p><label for="episode_nr"><?php echo _("Episode Number"); ?>:</label> <input style="width:25px" name="episode_nr" type="text" id="episode_nr" class="{required:true,digits:true}" value="<?=$episode_nr;?>" tabindex="1" /></p>
		
		<p class="get_episode"><input type="submit" name="get" class="get" id="get" value="<?php echo _("Import episode data"); ?>" /> <?php echo _("or edit bellow"); ?>:</p>
	
		<p><label for="title"><?php echo _("Title"); ?>:</label> <input name="title" style="width:400px" type="text" id="title" value="<?=$title;?>" tabindex="2" /></p>
		<p><label for="description"><?php echo _("Description"); ?>:</label> <textarea name="description" id="description" rows="8" cols="40" tabindex="3"><?=$description;?></textarea></p>
	
		<p class="submit"><input type="submit" name="submit" class="submit" id="submit" value="<?php echo _("Edit Episode"); ?>" /> <?php echo _("or"); ?> <a href="index.php?s=<?=$show?>&amp;t=<?=$season?>"><?php echo _("Cancel"); ?></a></p>
	</form>
	
	<div id="import_notice">
		<p><?php echo _("<strong>Updating data...</strong><br />Please wait."); ?></p>
	</div>
	
	<div class="push"></div>

</div><!-- /#main -->
<?php include("library/footer.php");?>	
</body>
</html>
